//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 17
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace CallingGetEvents
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            // TODO:  You must supply a valid subscription id and watermark when calling
            // the CallGetEvents method below
            //
        }

        /// <summary>
        /// Call GetEvents and parse the results (Listing 17-10)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <param name="subscriptionId">Id of subscription to retrieve events for</param>
        /// <param name="watermark">REF Watermark - will be updated on return</param>
        /// <param name="newMailEvents">OUT List of new mail events</param>
        /// <param name="createdEvents">OUT List of created events</param>
        /// <param name="deletedEvents">OUT List of deleted events</param>
        /// <param name="movedEvents">OUT List of moved events</param>
        /// <param name="copiedEvents">OUT List of copied events</param>
        /// <param name="modifiedEvents">OUT List of modified events</param>
        /// 
        public static void CallGetEvents(
                                ExchangeServiceBinding binding,
                                string subscriptionId,
                                ref string watermark,
                                out List<BaseObjectChangedEventType> newMailEvents,
                                out List<BaseObjectChangedEventType> createdEvents,
                                out List<BaseObjectChangedEventType> deletedEvents,
                                out List<MovedCopiedEventType> movedEvents,
                                out List<MovedCopiedEventType> copiedEvents,
                                out List<ModifiedEventType> modifiedEvents)
        {
            // Create the OUT lists 
            //
            newMailEvents = new List<BaseObjectChangedEventType>();
            createdEvents = new List<BaseObjectChangedEventType>();
            deletedEvents = new List<BaseObjectChangedEventType>();
            movedEvents = new List<MovedCopiedEventType>();
            copiedEvents = new List<MovedCopiedEventType>();
            modifiedEvents = new List<ModifiedEventType>();

            // We want to deal with the case where GetEvents indicates that there are more
            // events available in the Event Queue, so call this in a loop until moreEvents
            // is false
            //
            bool moreEvents = true;
            while (moreEvents)
            {
                // Create the GetEvents request using the overloaded constructor
                //
                GetEventsType getEventsRequest = new GetEventsType(subscriptionId, watermark);

                // Now call GetEvents
                //
                GetEventsResponseType getEventsResponse = binding.GetEvents(getEventsRequest);
                GetEventsResponseMessageType getEventsResponseMessage =
                    getEventsResponse.ResponseMessages.Items[0] as GetEventsResponseMessageType;
                if (getEventsResponseMessage.ResponseCode != ResponseCodeType.NoError)
                {
                    throw new Exception("GetEvents failed with response code: " +
                             getEventsResponseMessage.ResponseCode.ToString());
                }

                NotificationType notifications = getEventsResponseMessage.Notification;
                for (int count = 0; count < notifications.Items.Length; count++)
                {
                    BaseNotificationEventType actualEvent = notifications.Items[count];
                    // Update the Watermark
                    //
                    watermark = notifications.Items[count].Watermark;
                    // Now parse the response and save off the events to 
                    // the appropriate List variable
                    //
                    switch (notifications.ItemsElementName[count])
                    {
                        case ItemsChoiceType.NewMailEvent:
                            newMailEvents.Add(actualEvent as BaseObjectChangedEventType);
                            break;
                        case ItemsChoiceType.CreatedEvent:
                            createdEvents.Add(actualEvent as BaseObjectChangedEventType);
                            break;
                        case ItemsChoiceType.DeletedEvent:
                            deletedEvents.Add(actualEvent as BaseObjectChangedEventType);
                            break;
                        case ItemsChoiceType.CopiedEvent:
                            copiedEvents.Add(actualEvent as MovedCopiedEventType);
                            break;
                        case ItemsChoiceType.MovedEvent:
                            movedEvents.Add(actualEvent as MovedCopiedEventType);
                            break;
                        case ItemsChoiceType.ModifiedEvent:
                            modifiedEvents.Add(actualEvent as ModifiedEventType);
                            break;
                        case ItemsChoiceType.StatusEvent:
                            // Don't need to do anything here. This is just a status event
                            //
                            break;
                    }
                }
            }
        }
    }
}
